﻿Imports ITClassLibrary
Imports System.Data.SqlClient

Public Class MainForm
    Dim WithEvents emu As New sfdc_emulator("0.0.0.0", "18000")
    Public enviado As String
    Dim counter As Integer = 0
    Public MyColour As Color

    Private Sub PressEnterKey(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtdato.KeyPress, TextBox1.KeyPress
        If e.KeyChar = Chr(13) Then
            enviado = txtdato.Text.Trim
            'El sfdc 1 no admite cadenas vacias, es necesario mandarle un espacio en blanco para que responda
            If enviado = "" Then
                enviado = " "
            End If
            If Not cbxunits.SelectedItem Is Nothing Then
                emu.sfdc_ip = My.Settings.sfdcip
                emu.Enviar(My.Settings.unit + "T" + enviado)
                txtdato.Text = ""
                txtdato.Focus()
            Else
                If cbxunits.Enabled = True Then
                    MsgBox("Please, select a valid option from the list!", MsgBoxStyle.OkOnly, "Error!")
                    txtdato.Text = ""
                    txtdato.Focus()
                Else
                    If sender.name = "TextBox1" Then
                        txtdato.Focus()
                        emu.sfdc_ip = My.Settings.sfdcip
                        emu.Enviar(My.Settings.unit + "T" + " ")
                    Else
                        emu.sfdc_ip = My.Settings.sfdcip
                        emu.Enviar(My.Settings.unit + "T" + enviado)
                        txtdato.Text = ""
                        txtdato.Focus()
                    End If
                End If
            End If
        End If
    End Sub

    Public Sub ActivateAdvice(ByVal color As Color)
        My.Settings.advicecolor = color
        tmrAdvice.Enabled = True
    End Sub

    Private Sub EmuRespuesta(ByVal dato As String) Handles emu.respuesta
        Dim array(), r1, r2, temp As String
        temp = ""
        array = Nothing
        Try
            If My.Settings.semaphore = True Then
                If dato.Contains("PASS to") Then
                    semaforo.Image = My.Resources.PASS
                    If My.Settings.bigadvice = True Then
                        ActivateAdvice(Color.Green)
                    End If
                ElseIf dato.Contains("Flow Error") Then
                    semaforo.Image = My.Resources._ERROR
                    If My.Settings.bigadvice = True Then
                        ActivateAdvice(Color.Yellow)
                    End If
                    ElseIf dato.Contains("Wrong r") Then
                    semaforo.Image = My.Resources.FAIL
                    If My.Settings.bigadvice = True Then
                        ActivateAdvice(Color.Red)
                    End If
                    ElseIf dato.Contains("FAIL to") Then
                    semaforo.Image = My.Resources.FAIL
                    If My.Settings.bigadvice = True Then
                        ActivateAdvice(Color.Red)
                    End If
                    Else
                        semaforo.Image = My.Resources.SCAN
                    Me.BackColor = MyColour
                    End If
            Else
                semaforo.Visible = False
            End If
            array = Split(emu.procesar(dato), "@")
            temp = TextBox1.Text
            r1 = array(0)
            r2 = array(1)
            If array.Length = 4 Then
                'TextBox1.Text = r1 & Constants.vbCrLf & enviado & Constants.vbCrLf & r2 & Constants.vbCrLf & array(2) & Constants.vbCrLf & array(3)
                ' TextBox1.Text = TextBox1.Text & Constants.vbCrLf & Constants.vbCrLf & temp
                'AppendText(Constants.vbCrLf & Constants.vbCrLf & enviado & Constants.vbCrLf & r2 & Constants.vbCrLf & r1 & Constants.vbCrLf & array(2) & Constants.vbCrLf & array(3))
                AppendText(Constants.vbCrLf & Constants.vbCrLf & enviado)
                AppendText(Constants.vbCrLf & r2)
                AppendText(Constants.vbCrLf & r1)
                AppendText(Constants.vbCrLf & array(2))
                AppendText(Constants.vbCrLf & array(3))
                'AppendText(
            Else
                'TextBox1.Text = temp & Constants.vbCrLf & Constants.vbCrLf & enviado & Constants.vbCrLf & r1 & Constants.vbCrLf & r2
                'AppendText(Constants.vbCrLf & Constants.vbCrLf & enviado & Constants.vbCrLf & r1 & Constants.vbCrLf & r2)
                AppendText(Constants.vbCrLf & Constants.vbCrLf & enviado)
                AppendText(Constants.vbCrLf & r2)
                AppendText(Constants.vbCrLf & r1)
            End If
        Catch e As Exception When e.Message.Contains("Index was outside the bounds")
            TextBox1.Text = array(0) & Constants.vbCrLf & enviado & Constants.vbCrLf & Constants.vbCrLf & temp
        End Try
        Posicionar()
    End Sub

    Private Sub AppendText(ByVal sText As String)
        With Me.TextBox1
            .SelectionStart = .Text.Length
            .SelectedText = sText '& vbCrLf
            .SelectionStart = .Text.Length
        End With
    End Sub

    Private Sub AboutBtn()
        About.ShowDialog()
    End Sub

    Private Sub ConfigBtn()
        Login.txtpassword.Text = ""
        Login.ShowDialog()
    End Sub

    Private Sub MainFormLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MyColour = My.Settings.backcolor
        CheckForIllegalCrossThreadCalls = False
        TextBox1.ForeColor = My.Settings.textcolor
        TextBox1.BackColor = My.Settings.textbackcolor
        Me.Text = Me.Text + " V" + My.Application.Info.Version.ToString
        If My.Settings.logo <> "" Then
            piclogomain.ImageLocation = "http://143.116.204.27/SFDCADMIN/LOGOS/" + My.Settings.logo
        End If

        If My.Settings.custom = False Then
            lblsfdcip.Text = My.Settings.sfdcip
            lbldevice.Text = My.Settings.unit
        End If
        If My.Settings.custom = True Then
            cbxunits.Enabled = True
            ToolStripStatusLabel5.Visible = True
            lblprofile.Visible = True
            lblprofile.Text = My.Settings.profilename
            Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
            Dim cmd As New SqlCommand("SELECT     Emu_Configs.nombre FROM         Emu_Configs INNER JOIN                      Perfiles ON Emu_Configs.id_perfil = Perfiles.id_perfil WHERE     (Perfiles.nombre = '" + My.Settings.profilename + "') order by Emu_Configs.nombre", cnn)
            Dim reader As SqlDataReader
            Using cnn
                cnn.Open()
                reader = cmd.ExecuteReader
                While reader.Read
                    cbxunits.Items.Add(reader(0).ToString.Trim)
                End While
                reader.Close()
            End Using
        Else
            cbxunits.Enabled = False
            lblprofile.Visible = False
            ToolStripStatusLabel5.Visible = False
            lblprofile.Text = ""
        End If
        Me.Size = My.Settings.tam
        txtdato.Focus()
    End Sub

    Private Sub CbxUnitsChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxunits.TextChanged
        Try
            Dim cnn As New SqlConnection("Data Source=143.116.204.27;Initial Catalog=Emulator;Persist Security Info=True;User ID=gerardo_martinez;Password=Sanmina01")
            Dim cmd As New SqlCommand("SELECT     Emu_Configs.device, Emu_Configs.ip FROM         Emu_Configs WHERE     (Emu_Configs.nombre = @name)", cnn)
            cmd.Parameters.AddWithValue("@name", cbxunits.SelectedItem.ToString.Trim)
            Dim reader As SqlDataReader
            Using cnn
                cnn.Open()
                reader = cmd.ExecuteReader
                If reader.Read Then
                    My.Settings.unit = Config.Return_Unit(reader(0).ToString.Trim)
                    lbldevice.Text = My.Settings.unit
                    My.Settings.sfdcip = reader(1).ToString.Trim
                    lblsfdcip.Text = My.Settings.sfdcip
                    My.Settings.Save()
                End If
            End Using
            lblselectedunit.Text = cbxunits.SelectedItem.ToString
            txtdato.Focus()
        Catch ex As NullReferenceException

        End Try
    End Sub

    Private Sub MainFormSizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.SizeChanged
        If Me.Size.Height <> 500 Or Me.Size.Width <> 360 Then
            My.Settings.tam = Me.Size
            My.Settings.Save()
        End If
        txtdato.Focus()
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ConfigurationToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfigurationToolStripMenuItem.Click, ConfigurationToolStripMenuItem1.Click
        ConfigBtn()
    End Sub

    Private Sub Posicionar()
        TextBox1.SelectionStart = TextBox1.Text.Length
    End Sub
 
    Private Sub tmrAdvice_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrAdvice.Tick
        counter += 1

        tmrAdvice.Interval = 500

        If My.Settings.backcolor = MyColour Then
            My.Settings.backcolor = My.Settings.advicecolor
        Else
            My.Settings.backcolor = MyColour
        End If

        If counter >= 9 Then
            tmrAdvice.Enabled = False
            counter = 0

        End If

    End Sub

    Private Sub AcercaDeAdvancesSFDCEmulatorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AcercaDeAdvancesSFDCEmulatorToolStripMenuItem.Click
        AboutBtn()
    End Sub


End Class
